/*
 * ReportPanel.java
 *
 * Created on 14 de Mar�o de 2003, 20:04
 */
package saci.reptil;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.LayoutManager;

import java.util.HashMap;

import javax.swing.JPanel;

/**
 * Classe abstrata que representa um painel do relat�rio
 *
 * @author  saci
 */
public abstract class ReportPanel extends JPanel {

    private HashMap components = new HashMap();
    private Object constraint;

    /**
     * Cria uma inst�ncia da classe
     */
    public ReportPanel() {
        this(new FlowLayout());
    }

    /**
     * Cria uma inst�ncia da classe utilizado o <i>layout</i> do parametro
     */
    public ReportPanel(LayoutManager layout) {
        super();
        setBackground(Color.white);
        setLayout(layout);
    }

    /**
     * Desenha os componentes do painel
     */
    public void paint(Graphics g) {
        doLayout();

        try {
            super.paint(g);
        } catch (Exception e) {
            try {
                if (getBorder() != null) {
                    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
                }
            } catch (Exception err) {
            }
        }

        Component[] children = super.getComponents();

        Graphics cGraphic;

        for (int i = 0; i < children.length; i++) {
            if (children[i].getSize().height > 0) {
                cGraphic = g.create(children[i].getLocation().x, children[i].getLocation().y, children[i].getWidth(), children[i].getHeight());
                cGraphic.setColor(children[i].getBackground());
                cGraphic.fillRect(0, 0, children[i].getWidth(), children[i].getHeight());
                cGraphic.setColor(Color.black);
                children[i].paint(cGraphic);
            }
        }
    }

    /**
     * Altera o nome de um componente
     *
     * @param c o componente
     * @param name o novo nome
     */
    public void setComponentName(Component c, String name) {
        insertComponent(c);
    }

    /**
     * Retorna um componente
     */
    public Component getComponentByName(String name) {
        return (Component) components.get(name.toUpperCase());
    }

    /**
     * Retorna um <i>InnerPanel</i>
     */
    public InnerPanel getInnerPanel(String name) {
        return (InnerPanel) components.get(name.toUpperCase());
    }

    /**
     * Adiciona um componente
     */
    public Component add(Component c) {
        insertComponent(c);
        return super.add(c);
    }

    /**
     * Adiciona um componente
     */
    public Component add(Component c, int index) {
        insertComponent(c);
        return super.add(c, index);
    }

    /**
     * Adiciona um componente
     */
    public Component add(String name, Component c) {
        insertComponent(c, name);
        return super.add(name, c);
    }

    /**
     * Adiciona um componente
     */
    public void add(Component c, Object constraint) {
        insertComponent(c);
        super.add(c, constraint);
    }

    /**
     * Adiciona um componente
     */
    public void add(Component c, Object constraint, int index) {
        insertComponent(c);
        super.add(c, constraint, index);
    }

    private void insertComponent(Component c) {
        insertComponent(c, null);
    }

    private void insertComponent(Component c, String name) {
        if (name == null) {
            if (c.getName() != null && c.getName().trim().length() > 0) {
                components.put(c.getName().toUpperCase(), c);
            }
        } else {
            if (components.containsValue(c)) {
                components.remove(c);
            }

            components.put(name.toUpperCase(), c);
        }
    }

    /**
     * Altera a altura do painel
     */
    public void setHeight(int height) {
        setSize(getWidth(), height);
    }

    /**
     * Altera a <i>constraint</i> que ser� utilizada pelo <i>layout</i>
     */
    public void setConstraint(Object constraint) {
        this.constraint = constraint;
    }

    /**
     * Retorna a <i>constraint</i> que ser� utilizada pelo <i>layout</i>
     */
    public Object getConstraint() {
        return constraint;
    }

    /*public void prepareFunctions(int page, int position) {
    prepareFunctions(page, -1, position);
    }*/
    /**
     * Prepara as fun��es
     *
     * @param page o n�mero da p�gina sendo impressa
     * @param numberOfPages o n�mero total de p�ginas do relat�rio
     * @param position a posi��o dos dados no banco (linha)
     */
    public void prepareFunctions(int page, int numberOfPages, int position) {
        Component[] c = getComponents();

        Label lbl;
        String text;
        for (int i = 0; i < c.length; i++) {
            if (c[i] instanceof Label) {
                lbl = (Label) c[i];

                switch (lbl.getFunction()) {
                    case Label.NOW:
                        lbl.setText(saci.reptil.util.Util.obtemData("dd/MM/yyyy hh:mm:ss"));
                        break;
                    case Label.DATE:
                        lbl.setText(saci.reptil.util.Util.obtemData("dd/MM/yyyy"));
                        break;
                    case Label.TIME:
                        lbl.setText(saci.reptil.util.Util.obtemData("hh:mm:ss"));
                        break;
                    case Label.PAGE_NUMBER:
                        lbl.setText(String.valueOf(page + 1));
                        break;
                    case Label.NUMBER_OF_PAGES:
                        lbl.setText(String.valueOf(numberOfPages));
                        break;
                    case Label.DATABASE_POSITION:
                        lbl.setText(String.valueOf(position));
                        break;
                }

                if ((text = lbl.executeOtherFunction()) != null) {
                    lbl.setText(text);
                }
            } else if (c[i] instanceof ReportPanel) {
                ((ReportPanel) c[i]).prepareFunctions(page, numberOfPages, position);
            }
        }
    }
}
